package com.guoq.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.guoq.constant.Configration;
import com.guoq.pojo.Address;
import com.guoq.pojo.AddressExample;
import com.guoq.pojo.EUDataGridResult;
import com.guoq.pojo.Result;
import com.guoq.pojo.User;
import com.guoq.service.IAddressService;

/**
 * 
 * <P>类名称:AddressController
 * <P>类描述:  收货地址Controller
 * <P>创建人:郭强  
 * <P>修改人:XXX 
 * <P>修改时间:2017年7月19日 上午11:45:35
 * <P>修改备注: 
 * @version 1.0.0
 * Copyright(c)2017西部在线-版权所有
 */
@Controller
@RequestMapping("/address")
public class AddressController {
	@Resource
	private IAddressService addressService;
	
	@RequestMapping("")
	public ModelAndView toList(HttpServletRequest request,Model model) {
		
		User user = (User) request.getSession().getAttribute("user");
		String user_id = user.getUserId();
		ModelAndView modelAndView = new ModelAndView();
		EUDataGridResult result = this.addressService.getList(user_id);
		modelAndView.addObject("result", result);
		modelAndView.setViewName("/address/list");
		return modelAndView;
	}
	
	@RequestMapping("/toadd")
	public ModelAndView toadd(HttpServletRequest request,Model model){
		
		ModelAndView modelAndView = new ModelAndView();
		String userId = request.getParameter("userid");
		
		AddressExample example = new AddressExample();
		int total = this.addressService.countByExample(example, userId);
		Result result = new Result();
		if(total >= 10){
			result.setStatus(500);
			result.setMsg("最多只能创建10个地址");
		}else{
			result.setStatus(500);
		}
		modelAndView.addObject("result", result);
		modelAndView.setViewName("/address/add");
		return modelAndView;
	}
	
	@RequestMapping("/toedit")
	public String toedit(HttpServletRequest request,Model model){
		return "/address/edit";
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: list
	 *<P>描述: 获取列表信息
	 *@param request
	 *@param model
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/list")
	@ResponseBody
	public EUDataGridResult list(HttpServletRequest request,Model model){
		
		User user = (User) request.getSession().getAttribute("user");
		String user_id = user.getUserId();
		EUDataGridResult list = this.addressService.getList(user_id);
//		List<Address> list = this.addressService.getList("15022971992");
		return list;
	}	
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: getById
	 *<P>描述: 根据id获取实体信息
	 *@param request
	 *@param model
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/getById")
	@ResponseBody
	public Address getById(HttpServletRequest request,Model model){
		
		int id = Integer.parseInt(request.getParameter("id"));
		Address record = this.addressService.getById(id);
		return record;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: add
	 *<P>描述: 新增操作
	 *@param bean
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public Result add(HttpServletRequest request, Address bean) throws Exception{
		
		User user = (User) request.getSession().getAttribute("user");
		String user_id = user.getUserId();
		
		AddressExample example = new AddressExample();
		int total = this.addressService.countByExample(example, user_id);
		Result result = new Result();
		if(total >= Configration.ADDRESS){
			result.setStatus(500);
			result.setMsg("最多只能创建" + Configration.ADDRESS + "个地址");
		}else{
			result = this.addressService.create(bean);
		}
		
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: update
	 *<P>描述: 编辑操作
	 *@param bean
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public Result update(Address bean) throws Exception{
		
		Result result = this.addressService.update(bean);
		return result;
	} 
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: delete
	 *<P>描述: 删除操作
	 *@param bean
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public Result delete(Address bean) throws Exception{
		
		Result result = this.addressService.delete(bean);
		return result;
	} 
}
